<template>
  <div class="xtx-form">
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <input class="input" v-model.trim="form.account" type="text" placeholder="请输入用户名" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input class="input" v-model.trim="form.mobile" type="text" placeholder="请输入手机号" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input class="input" v-model.trim="form.code" type="text" placeholder="请输入验证码" />
        <span class="code" @click="sendCode()">{{countTime===0?"发送验证码":`${countTime}秒后尝试`}}</span>
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input class="input" v-model.trim="form.password" type="password" placeholder="请输入密码" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input class="input" v-model.trim="form.rePassword" type="password" placeholder="请确认密码" />
      </div>
      <div class="error"></div>
    </div>
    <a href="javascript:;" class="submit" @click="submit">立即提交</a>
  </div>
</template>

<script>
import { reactive } from 'vue-demi'
import { message } from '@/components/Message'
import { reqUserQQPatchCode, reqUserQQPatchLogin } from '@/api/user'
import { useCountDown } from '@/compositions'
export default {
  name: 'CallbackPatch',
  props: {
    unionId: {
      type: String
    }
  },
  setup (props) {
    // 调用倒计时函数 countTime倒计时数字，start启动倒计时的函数
    const { start, countTime } = useCountDown(60)
    // 表单对象
    const form = reactive({ mobile: '17840061370', code: '', account: '', password: '', rePassword: '' })
    const sendCode = async () => {
      // 校验收集号码，有错误退出函数并提示用户
      if (!/^1[3-9]\d{9}$/.test(form.mobile)) return message({ text: '手机号码格式不正确' })
      // 发送手机验证码
      await reqUserQQPatchCode(form.mobile)
      // 发送验证码成功后启动倒计时
      start()
    }
    // 提交用户注册的信息
    const submit = async () => {
      // 判断用户输入的两次信息是否一致
      if (!form.password === form.rePassword) return message({ text: '密码不一致' })
      // 发送请求获取用户注册后的信息
      const { result } = await reqUserQQPatchLogin({ ...form, unionId: props.unionId })
      console.log('result------->', result)
    }
    return { form, sendCode, countTime, submit }
  }
}
</script>

<style scoped lang='less'>
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
